package singleton;

/**
 * @description:
 * @author: jhy
 * @time: 2021/4/22 21:12
 */


public class LazySingletonNoSafeTest {

    public static void main(String[] args) {
        //System.out.println(LazySingletonNoSafe.getInstance() == LazySingletonNoSafe.getInstance());
        new Thread(()->{
            try {
                System.out.println(LazySingletonNoSafe.getInstance());
            } catch (InterruptedException exception) {
                exception.printStackTrace();
            }
        }).start();

        new Thread(()->{
            try {
                System.out.println(LazySingletonNoSafe.getInstance());
            } catch (InterruptedException exception) {
                exception.printStackTrace();
            }
        }).start();
    }


}

//懒汉式实现单例模式 ---->线程不安全
class LazySingletonNoSafe{

    private static LazySingletonNoSafe instance;

    //私有构造器
    private LazySingletonNoSafe(){
    }

    //对外提供一个全局访问点，拿到该对象
    public static LazySingletonNoSafe getInstance() throws InterruptedException {
        if(instance == null){
            Thread.sleep(2000);
            instance = new LazySingletonNoSafe();
            return instance;
        }
        return instance;
    }
}


